package com.ssm.basis.web.aop;

import com.ssm.basis.web.servlet.filter.RequestLogFilter;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;

import java.time.Duration;
import java.time.Instant;

/**
 * 耗时统计
 * https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#aop
 *
 * @see RequestLogFilter
 */
@Slf4j
@Aspect
// @Component
class ControllerAspect {
    @Around("execution(* com.ssm.*.controller..*.*(..))")
    Object executionTime(ProceedingJoinPoint point) throws Throwable {
        Instant start = Instant.now();
        Object result = point.proceed();
        Instant finish = Instant.now();
        long timeElapsed = Duration.between(start, finish).toMillis();
        log.debug("{} execution time: {} ms", point.getSignature(), timeElapsed);
        return result;
    }
}
